package com.lizicloud.infrastructure.persistence;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lizicloud.domain.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 用户Mapper接口
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
    
    /**
     * 根据用户账号查询用户
     * @param useraccount 用户账号
     * @return 用户实体
     */
    @Select("SELECT id, useraccount, username, password, email, phone, avatar, role, status, last_login_time, last_login_ip, created_time, updated_time, remark, is_delete FROM users WHERE useraccount = #{useraccount} AND is_delete = 0")
    User selectByUseraccount(@Param("useraccount") String useraccount);
    
    /**
     * 根据邮箱查询用户
     * @param email 邮箱
     * @return 用户实体
     */
    @Select("SELECT id, useraccount, username, password, email, phone, avatar, role, status, last_login_time, last_login_ip, created_time, updated_time, remark, is_delete FROM users WHERE email = #{email} AND is_delete = 0")
    User selectByEmail(@Param("email") String email);
    
    /**
     * 更新用户最后登录信息
     * @param id 用户ID
     * @param loginTime 登录时间
     * @param loginIp 登录IP
     */
    void updateLoginInfo(@Param("id") Long id, 
                        @Param("loginTime") java.time.LocalDateTime loginTime, 
                        @Param("loginIp") String loginIp);

    /** 统计未删除的用户总数 */
    @Select("SELECT COUNT(*) FROM users WHERE is_delete = 0")
    long countActiveUsers();

    /** 统计未删除的管理员数量 */
    @Select("SELECT COUNT(*) FROM users WHERE is_delete = 0 AND role = 'ADMIN'")
    long countActiveAdmins();

    /** 统计未删除的普通用户数量 */
    @Select("SELECT COUNT(*) FROM users WHERE is_delete = 0 AND role = 'USER'")
    long countActiveNormalUsers();

    /** 统计已删除的用户数量 */
    @Select("SELECT COUNT(*) FROM users WHERE is_delete = 1")
    long countDeletedUsers();
}